package cn.tannn.tcache.core;

import cn.tannn.tcache.command.CommandCommand;
import cn.tannn.tcache.command.InfoCommand;
import cn.tannn.tcache.command.KeysCommand;
import cn.tannn.tcache.command.PingCommand;
import cn.tannn.tcache.command.hash.*;
import cn.tannn.tcache.command.list.*;
import cn.tannn.tcache.command.set.*;
import cn.tannn.tcache.command.string.*;
import cn.tannn.tcache.command.zset.*;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 命令集合点
 *
 * @author <a href="https://t.tannn.cn/">tnnn</a>
 * @version V1.0
 * @date 2024/6/19 下午8:12
 */
public class Commands {

    public static Map<String, Command> ALL = new LinkedHashMap<>();

    static {
        initCommands();
    }

    /**
     * 初始化 命令
     * <p> 每新增一个命令的处理都要在这里注册一下
     */
    private static void initCommands() {
        // 基础命令
        register(new CommandCommand());
        register(new PingCommand());
        register(new InfoCommand());
        register(new KeysCommand());

        // string
        register(new SetCommand());
        register(new GetCommand());
        register(new StrlenCommand());
        register(new DelCommand());
        register(new ExistsCommand());
        register(new MgetCommand());
        register(new MsetCommand());
        register(new IncrCommand());
        register(new DecrCommand());

        // list
        register(new LpushCommand());
        register(new LpopCommand());
        register(new RpushCommand());
        register(new RpopCommand());
        register(new LlenCommand());
        register(new LindexCommand());
        register(new LrangeCommand());

        // set 数据不重复
        register(new SaddCommand());
        register(new SmembersCommand());
        register(new ScardCommand());
        register(new SismemberCommand());
        register(new SremCommand());
        register(new SpopCommand());

        // hash
        register(new HdelCommand());
        register(new HexistsCommand());
        register(new HgetallCommand());
        register(new HgetCommand());
        register(new HlenCommand());
        register(new HmgetCommand());
        register(new HsetCommand());

        // zset(sorted set) 有序集合
        register(new ZaddCommand());
        register(new ZcardCommand());
        register(new ZcountCommand());
        register(new ZrankCommand());
        register(new ZremCommand());
        register(new ZscoreCommand());
    }

    /**z
     * 注册命令
     * @param command Command
     */
    public static void register(Command command){
        ALL.put(command.name(),command);
    }


    /**
     * 获取命令
     * @param name 命令名
     */
    public static Command get(String name){
        return ALL.get(name);
    }


    /**
     * 获取所有命令
     */
    public static String[] getCommandNames() {
        return ALL.keySet().toArray(new String[0]);
    }
}
